package com.gl.library.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Author :hmc on 2016/11/11 2326.
 * Description: 毫秒换成00:00:00
 */
public class TimeTools {
    /**
     * 把毫秒数转换成时分秒
     */
    public static String getCountTimeByLong(long finishTime) {
        int totalTime = (int) (finishTime / 1000);//秒
        int days = 0 , hour = 0, minute = 0, second = 0;

        if (3600 <= totalTime) {
            hour = totalTime / 3600;
            totalTime = totalTime - 3600 * hour;
        }
        if (60 <= totalTime) {
            minute = totalTime / 60;
            totalTime = totalTime - 60 * minute;
        }
        if (0 <= totalTime) {
            second = totalTime;
        }
        StringBuilder sb = new StringBuilder();

        if (hour < 10) {
            sb.append("0").append(hour).append(":");
        } else {
            sb.append(hour).append(":");
        }
        if (minute < 10) {
            sb.append("0").append(minute).append(":");
        } else {
            sb.append(minute).append(":");
        }
        if (second < 10) {
            sb.append("0").append(second);
        } else {
            sb.append(second);
        }
        return sb.toString();

    }

    public static String formatLongToTimeStr(Long l) {
//        int totalTime = (int) (l / 1000);//秒
//        int hour = 0, minute = 0, second = 0;
        long days = l / (1000 * 60 * 60 * 24);
        long hours = (l % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (l % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (l % (1000 * 60)) / 1000;
//        if (3600 <= totalTime) {
//            hour = totalTime / 3600;
//            totalTime = totalTime - 3600 * hour;
//        }
//        if (60 <= totalTime) {
//            minute = totalTime / 60;
//            totalTime = totalTime - 60 * minute;
//        }
//        if (0 <= totalTime) {
//            second = totalTime;
//        }
        StringBuilder sbDay = new StringBuilder();
        StringBuilder sbHour = new StringBuilder();
        StringBuilder sbMinute = new StringBuilder();
        StringBuilder sbSecond = new StringBuilder();
        if (days < 10) {
            sbDay.append("0").append(days);
        } else {
            sbDay.append(days);
        }
        if (hours < 10) {
            sbHour.append("0").append(hours);
        } else {
            sbHour.append(hours);
        }
        if (minutes < 10) {
            sbMinute.append("0").append(minutes);
        } else {
            sbMinute.append(minutes);
        }
        if (seconds < 10) {
            sbSecond.append("0").append(seconds);
        } else {
            sbSecond.append(seconds);
        }
        String strtime = sbHour + ":" + sbMinute + ":" + sbSecond;
        return strtime;
    }

    public static String formatLongToHourStr(Long l) {
        int totalTime = (int) (l / 1000);//秒
        int hour = 0, minute = 0, second = 0;
//        long hours = (l % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
//        long minutes = (l % (1000 * 60 * 60)) / (1000 * 60);
//        long seconds = (l % (1000 * 60)) / 1000;
        if (3600 <= totalTime) {
            hour = totalTime / 3600;
            totalTime = totalTime - 3600 * hour;
        }
        if (60 <= totalTime) {
            minute = totalTime / 60;
            totalTime = totalTime - 60 * minute;
        }
        if (0 <= totalTime) {
            second = totalTime;
        }

        StringBuilder sbHour = new StringBuilder();
        StringBuilder sbMinute = new StringBuilder();
        StringBuilder sbSecond = new StringBuilder();
        if (hour < 10) {
            sbHour.append("0").append(hour);
        } else {
            sbHour.append(hour);
        }
        if (minute < 10) {
            sbMinute.append("0").append(minute);
        } else {
            sbMinute.append(minute);
        }
        if (second < 10) {
            sbSecond.append("0").append(second);
        } else {
            sbSecond.append(second);
        }
        String strtime = sbHour + ":" + sbMinute + ":" + sbSecond;
        return strtime;
    }

    public static String formatLongToMinStr(Long l) {
//        int totalTime = (int) (l / 1000);//秒
//        int hour = 0, minute = 0, second = 0;
        long days = l / (1000 * 60 * 60 * 24);
        long hours = (l % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (l % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (l % (1000 * 60)) / 1000;
//        if (3600 <= totalTime) {
//            hour = totalTime / 3600;
//            totalTime = totalTime - 3600 * hour;
//        }
//        if (60 <= totalTime) {
//            minute = totalTime / 60;
//            totalTime = totalTime - 60 * minute;
//        }
//        if (0 <= totalTime) {
//            second = totalTime;
//        }

        StringBuilder sbDay = new StringBuilder();
        StringBuilder sbHour = new StringBuilder();
        StringBuilder sbMinute = new StringBuilder();
        StringBuilder sbSecond = new StringBuilder();
//        if (days < 10) {
//            sbDay.append("0").append(days);
//        } else {
//            sbDay.append(days);
//        }
//        if (hours < 10) {
//            sbHour.append("0").append(hours);
//        } else {
//            sbHour.append(hours);
//        }
//        if (minutes < 10) {
//            sbMinute.append("").append(minutes);
//        } else {
//            sbMinute.append(minutes);
//        }
//        if (seconds < 10) {
//            sbSecond.append("0").append(seconds);
//        } else {
//            sbSecond.append(seconds);
//        }
        sbMinute.append(minutes);
        String strtime = sbMinute + "";
        return strtime;
    }

    public static String formatLongToDayStr(Long l) {
//        int totalTime = (int) (l / 1000);//秒
//        int hour = 0, minute = 0, second = 0;
        long days = l / (1000 * 60 * 60 * 24);
        long hours = (l % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (l % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (l % (1000 * 60)) / 1000;
//        if (3600 <= totalTime) {
//            hour = totalTime / 3600;
//            totalTime = totalTime - 3600 * hour;
//        }
//        if (60 <= totalTime) {
//            minute = totalTime / 60;
//            totalTime = totalTime - 60 * minute;
//        }
//        if (0 <= totalTime) {
//            second = totalTime;
//        }

        StringBuilder sbDay = new StringBuilder();
        StringBuilder sbHour = new StringBuilder();
        StringBuilder sbMinute = new StringBuilder();
        StringBuilder sbSecond = new StringBuilder();
        if (days < 10) {
            sbDay.append("0").append(days);
        } else {
            sbDay.append(days);
        }
        if (hours < 10) {
            sbHour.append("0").append(hours);
        } else {
            sbHour.append(hours);
        }
        if (minutes < 10) {
            sbMinute.append("0").append(minutes);
        } else {
            sbMinute.append(minutes);
        }
        if (seconds < 10) {
            sbSecond.append("0").append(seconds);
        } else {
            sbSecond.append(seconds);
        }
        String strtime = sbDay + "天" + sbHour + "时";
        return strtime;

    }

    public static String time2Stamp(String time,String type) throws ParseException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        date = format.parse(time);
        SimpleDateFormat format1 = new SimpleDateFormat(type);
        String s = format1.format(date);
        return s;

    }

    public static long timeToMillisecond(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date.getTime()/1000;
    }

    public static boolean isTimeRange() {
        Calendar cal = Calendar.getInstance();// 当前日期
        int hour = cal.get(Calendar.HOUR_OF_DAY);// 获取小时
        int minute = cal.get(Calendar.MINUTE);// 获取分钟
        int minuteOfDay = hour * 60 + minute;// 从0:00分开是到目前为止的分钟数
        final int start = 8 * 60 + 40;// 起始时间 8:40的分钟数
        final int end = 20 * 60;// 结束时间 20:00的分钟数
        if (minuteOfDay >= start && minuteOfDay <= end) {
            System.out.println("在外围内");
            return true;
        } else {
            System.out.println("在外围外");
            return false;
        }
    }

    public static String timeStamp2Date(long time, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(time * 1000));
    }
}
